Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support to load kubernetes configuration for memory buffer #242

Merged

Conversation

DanyT
Copy link
Contributor

@DanyT DanyT commented Jul 1, 2024

  • existing api is not changed
    • load_kube_config works as before
  • extend kubeconfig_t to hold in memory buffer
  • make a common flow for file/buffer code
  • set fileName/buffer members accordingly
  • provide new api: load_kube_config_buffer

- existing api is not changed
   - load_kube_config works as before
- extend kubeconfig_t to hold in memory buffer
- make a common flow for file/buffer code
- set fileName/buffer members accordingly
- provide new api: load_kube_config_buffer
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 1, 2024
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 1, 2024
close filehandle only if we opened one
@ityuhui
Copy link
Member

ityuhui commented Jul 2, 2024

Thanks for your PR. I will review it.

static char fname[] = "load_kube_config()";
int rc = 0;

kubeconfig_t *kubeconfig = kubeconfig_create();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that you delete this below, I think you can stack allocate this and pass the pointer via & below.

That will save a heap allocation and also be cleaner code.

Copy link
Contributor Author

@DanyT DanyT Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done but implies additional changes in kubeconfig_free() as this function will attempt to free the kubeconfig_t* itself. I can do a kubeconfig_free_static() if needed in order to keep the original api interface unchanged.

Please let me know your thoughts on this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meanwhile I added support to use both heap-based and stack-based allocated kubeconfig_t and a bit of documentation for these internal functions.

static char fname[] = "load_kube_config_buffer()";
int rc = 0;

kubeconfig_t *kubeconfig = kubeconfig_create();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stack allocate here to, as above.

Copy link
Contributor Author

@DanyT DanyT Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -24,13 +24,15 @@ extern "C" {
*
* IN:
* kubeconfig->fileName: kubernetes cluster configuration file name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe note that either one, but not both should be supplied?

Copy link
Contributor Author

@DanyT DanyT Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a more explicit note there. There was already a hint in the doc for kubeconfig->buffer.

  • IN:
  • kubeconfig->fileName: kubernetes cluster configuration file name
  • kubeconfig->buffer: kubernetes cluster configuration data; this is considered only if kubeconfig-> fileName is set to NULL
  • Note: One may use either kubeconfig->fileName or kubeconfig->buffer but not both at the same time.

@@ -430,25 +430,34 @@ int kubeyaml_load_kubeconfig(kubeconfig_t * kubeconfig)
{
static char fname[] = "kubeyaml_load_kubeconfig()";

/* Set a file input. */
/* Set a file input or use the provided buffer. */
FILE *input = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test here to verify that only one of fileName and buffer is non-null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@brendandburns
Copy link
Contributor

Minor comments.

DanyT added 2 commits July 3, 2024 09:18
…uffer is set

Add documentation note stating that only of the kubeconfig_t::fileName and kubeconfig_t::buffer may be set
@brendandburns
Copy link
Contributor

This looks good to me. Thanks for the quick turn around. I'll wait for @ityuhui to take a look though in case there are additional comments.

@ityuhui
Copy link
Member

ityuhui commented Jul 5, 2024

Thanks @brendandburns

@DanyT Can you please fix the issues reported by the code-static-check and code-sytle-check https://github.com/kubernetes-client/c/actions/runs/9781006686/job/27005098287?pr=242 in your code changes ? (Just update your code changes)

You can run the checks locally with

sh ./code-check/code-static-check.sh ./kubernetes/config/

and

find ./kubernetes/config/ -type f -regextype posix-extended -regex ".*\.(c|h)" -exec sh ./code-check/code-style-check.sh {} \;

@ityuhui
Copy link
Member

ityuhui commented Jul 5, 2024

@DanyT
Could you please write an example that uses your new function so we can add it to the automated tests in case your functions are broken by future code changes. We also have memory leak testing in automated testing.

FYI

test:

@DanyT
Copy link
Contributor Author

DanyT commented Jul 5, 2024

@ityuhui I have added:

  • example for load_kube_config_buffer: ./example/list_pod_buffer
  • fixed static and style code checks.

Let me know if there is something that i missed.

@ityuhui
Copy link
Member

ityuhui commented Jul 5, 2024

@DanyT

It seems that the build failed

cd list_pod_buffer; make test
make[1]: Entering directory '/home/runner/work/c/c/examples/list_pod_buffer'
./list_pod_buffer_bin
Cannot get kubernetes configuration from file.
make[1]: *** [Makefile:11: test] Error 255
make[1]: Leaving directory '/home/runner/work/c/c/examples/list_pod_buffer'
make: *** [Makefile:40: test] Error 2
Error: Process completed with exit code 2.

@DanyT
Copy link
Contributor Author

DanyT commented Jul 5, 2024

Interesting... it is not the build itself that fails but at runtime it cannot find/load the kube config file. I will look into it. Is there a special location where the kube config file is delivered in this test environment? @ityuhui

@ityuhui
Copy link
Member

ityuhui commented Jul 6, 2024

Nothing special, the configuration is located in $HOME/.kube/config

FYI

homedir_env = getenv(ENV_HOME);

@DanyT
Copy link
Contributor Author

DanyT commented Jul 22, 2024

@ityuhui Sorry for the delay. This should fix the issue.

@ityuhui
Copy link
Member

ityuhui commented Jul 22, 2024

Thank you @DanyT

Can you add your example to the memory leak test ?Sorry for not reminding you earlier.

cd list_pod; make memcheck

@DanyT
Copy link
Contributor Author

DanyT commented Jul 22, 2024

Done

@ityuhui
Copy link
Member

ityuhui commented Jul 22, 2024

I have no question about this PR. Thanks to @DanyT for the contribution !

@brendandburns I'm OK with the changes.

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 22, 2024
@brendandburns
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 22, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, DanyT, ityuhui

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [brendandburns,ityuhui]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 35d313f into kubernetes-client:master Jul 22, 2024
8 checks passed
@DanyT DanyT deleted the feature/load-kubeconfig-from-memory branch July 23, 2024 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants